from ctaf_core.ctaf_factory import CtafFactory
from ctaf_core.bus_client_bundle.bus_client_bundle import BusClientBundle
from ctaf_core.ctaf_objects.base_biz_app import BaseBizApp
from ctaf_core.ctaf_objects.base_bundle import BaseBundle

from ctaf_core.ctaf_objects.base_event import BaseEvent
from ctaf_core.ctaf_objects.base_service_request import BaseServiceReq
from ctaf_core.ctaf_objects.base_service_response import BaseServiceResp
from ctaf_core.ctaf_objects.ctaf_general_types import EventHandlerType
from ctaf_core.ctaf_platform import CoreTradeAppFramework
from ctaf_core.ctaf_utils.local_service_call_util import LocalServiceCallUitl
from ctaf_core.event_driven_bundle.event_driven_bundle import EventDrivenBundle
from ctaf_core.ctaf_objects.base_exception import CTAFbaseException
from ctaf_core.log_trace_bundle.log_trace_bundle import LogTraceBundle
from ctaf_facade.enumerations.error_code_enum import ErrorCodeEnum
from ctaf_facade.enumerations.event_handler_exe_type_num import EventHandlerExeTypeEnum
from ctaf_facade.enumerations.event_type_enum import EventTypeEnum


def ctaf_service_call(req: BaseServiceReq) -> BaseServiceResp:
    ctaf = get_ctaf_instance()
    if LocalServiceCallUitl.is_local_service(req):
        return LocalServiceCallUitl.invoke_local_service(req)
    else:
        bundle = ctaf.get_bundle_by_name("BusClientBundle")
        if bundle and isinstance(bundle, BusClientBundle):
            return bundle.send_rpc_request(req)
        raise CTAFbaseException(ErrorCodeEnum.NO_TARGET_SERVICE,  f"TARGET SERVICE {req.get_called_service_name()} NOT FOUND")


def ctaf_pub_event(base_event: BaseEvent):
    ctaf = get_ctaf_instance()
    bundle = ctaf.get_bundle_by_name("EventDrivenBundle")
    if isinstance(bundle, EventDrivenBundle):
        return bundle.put_event(base_event)


def ctaf_reg_event_handler(handler: EventHandlerType, event_type: EventTypeEnum = None, execution_type: EventHandlerExeTypeEnum = EventHandlerExeTypeEnum.SYNC_HANDLER):
    ctaf = get_ctaf_instance()
    bundle = ctaf.get_bundle_by_name("EventDrivenBundle")
    if isinstance(bundle, EventDrivenBundle):
        return bundle.event_handler_register(handler, event_type, execution_type)


def ctaf_dereg_event_handler(handler: EventHandlerType, event_type: EventTypeEnum = None, execution_type: EventHandlerExeTypeEnum = EventHandlerExeTypeEnum.SYNC_HANDLER):
    ctaf = get_ctaf_instance()
    bundle = ctaf.get_bundle_by_name("EventDrivenBundle")
    if isinstance(bundle, EventDrivenBundle):
        return bundle.event_handler_unregister(handler, event_type, execution_type)


def ctaf_log_debug(msg: str = None, exception: CTAFbaseException = None):
    ctaf = get_ctaf_instance()
    bundle = ctaf.get_bundle_by_name("LogTraceBundle")
    if isinstance(bundle, LogTraceBundle):
        return bundle.debug(msg, exception)


def ctaf_log_info(msg: str = None, exception: CTAFbaseException = None):
    ctaf = get_ctaf_instance()
    bundle = ctaf.get_bundle_by_name("LogTraceBundle")
    if isinstance(bundle, LogTraceBundle):
        return bundle.info(msg, exception)


def ctaf_log_warn(msg: str = None, exception: CTAFbaseException = None):
    ctaf = get_ctaf_instance()
    bundle = ctaf.get_bundle_by_name("LogTraceBundle")
    if isinstance(bundle, LogTraceBundle):
        return bundle.warn(msg, exception)


def ctaf_log_error(msg: str = None, exception: CTAFbaseException = None):
    ctaf = get_ctaf_instance()
    bundle = ctaf.get_bundle_by_name("LogTraceBundle")
    if isinstance(bundle, LogTraceBundle):
        return bundle.error(msg, exception)


def get_ctaf_instance()-> CoreTradeAppFramework:
    ctaf = CtafFactory.get_ctaf_instance()
    if isinstance(ctaf,CoreTradeAppFramework):
        return ctaf

def get_bundle_by_name(self, bundle_name: str)-> BaseBundle:
    ctaf = CtafFactory.get_ctaf_instance()
    return ctaf.__bundles[bundle_name]

def get_biz_app_by_name(self, biz_app_name: str)-> BaseBizApp:
    ctaf = CtafFactory.get_ctaf_instance()
    return ctaf.__apps[biz_app_name]